package krypto.blindsignature.utils;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ByteUtils {

	public static List<byte[]> split(byte[] bytes, BigInteger maxValue) {
		List<byte[]> bytesList = new ArrayList<byte[]>();
		int bytesCount = (maxValue.bitLength() - 1)/8;
		
		final int maxChunks = (int) Math.ceil(bytes.length
				/ (double) bytesCount);
	
		for (int i = 0; i < maxChunks; i++) {
			int from = i * bytesCount;
			int to = Math.min(from + bytesCount, bytes.length);
			byte[] range = Arrays.copyOfRange(bytes, from, to);
			bytesList.add(range);
			System.out.println("iteracja");
			System.out.println(maxValue.toString());
			System.out.println(new BigInteger(range).toString());
			System.out.println("max bytes: " + bytesCount + " range bytes: " + range.length);
		}
		return bytesList;
	}

	public static BigInteger toBigInteger(String message) {
		return new BigInteger(message.getBytes());
	}

	public static String toString(BigInteger message) {
		return new String(message.toByteArray());
	}

}
